import { View } from "../../../../mvc/core/View";
import { ILayOut } from "../../../../../modules/interfaces/ILayOut";
import { IToolTipItem } from "../../../../../com/mui/manager/IToolTipItem";
import { LayerManager } from "../../../manager/LayerManager";
import { ToolTipsManager } from "../../../../../com/mui/manager/ToolTipsManager";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { StaticResUrl } from "../../../resource/StaticResUrl";
import { ItemData } from "../../../resource/info/ItemData";
import { TItemWeapon } from "../../../../../Message/Db/Tables/TItemWeapon";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { RepairAlert } from "./RepairAlert";
import { Cache } from "../../../cache/Cache";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EEquip } from "../../../../../Message/Public/EEquip";
import { Global } from "../../../../../com/gengine/global/Global";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../../extend/language/Language";
//class EquipmentRepair
    
    export  class EquipmentRepair extends View implements ILayOut, IToolTipItem
    {
        constructor()
        {
            super();
            this.layer = LayerManager.uiLayer;
            this.resetPosition();
            ToolTipsManager.register(this);
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onShowAlert);
            return;
        }

        public updateEquipment(arg1: Array<any>): void
        {
            this._aryEquipment = arg1;
            var loc1=false;
            loc1 = this.isHaveMinValueDurable(0.2);
            if (loc1) 
            {
                if (this._equipmentRole != null) 
                {
                    this.updateRepair(arg1);
                }
                else if (this._isloading == false) 
                {
                    this._isloading = true;
                    LoaderManager.instance.load(StaticResUrl.EquipmentRepair, this.onLoadedHandler, 3, arg1, null, this.onFailedHandler);
                }
                this.show();
            }
            else 
            {
                this.hide();
            }
            this._isUpdate = true;
            return;
        }

        /* internal  */updateRepair(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                loc1 = arg1[loc4];
                loc2 = this._equipmentRole.getChildByName("eq" + (loc4 + 1)) as Object /* flash.display.MovieClip */;
                if (loc1) 
                {
                    if (loc2) 
                    {
                        if ((loc3 = this.getDurable(loc1)) != 0) 
                        {
                            if (loc3 < 0.2) 
                            {
                                loc3 = 2;
                            }
                            else 
                            {
                                loc3 = 1;
                            }
                        }
                        else 
                        {
                            loc3 = 3;
                        }
                        loc2.gotoAndStop(loc3);
                    }
                }
                else if (loc2) 
                {
                    loc2.gotoAndStop(1);
                }
                ++loc4;
            }
            return;
        }

        /* internal  */getDurable(arg1: ItemData): number
        {
            var loc2=null;
            var loc1=arg1.itemExInfo.durable;
            if (loc1 > 0) 
            {
                loc2 = arg1.itemInfo.extend as TItemWeapon;
                if (loc2) 
                {
                    return loc1 / loc2.durableUpper;
                }
                return 1;
            }
            return 0;
        }

        /* internal  */onLoadedHandler(arg1: SWFInfo): void
        {
            this._isloading = false;
            if (arg1.clip) 
            {
                this._equipmentRole = arg1.clip.getChildAt(0) as Object /* flash.display.MovieClip */;
            }
            this.updateRepair(arg1.extData as Array<any>);
            if (this._equipmentRole && this.contains(this._equipmentRole) == false) 
            {
                this.addChild(this._equipmentRole);
            }
            return;
        }

        /* internal  */onFailedHandler(arg1): void
        {
            this._isloading = false;
            return;
        }

        /* internal  */onShowAlert(arg1: Object /* flash.events.MouseEvent */): void
        {
            RepairAlert.showRepair(Cache.instance.vip.isVIP, this.isBreak);
            return;
        }

        /* internal  */get isBreak(): Boolean
        {
            if (this._isUpdate) 
            {
                this._isBreak = this.isHaveMinValueDurable(0);
                this._isUpdate = false;
            }
            return this._isBreak;
        }

        /* internal  */isHaveMinValueDurable(arg1: number=0.2): Boolean
        {
            var loc2=null;
            if (!this._aryEquipment) 
            {
                return false;
            }
            var loc1=1;
            var loc3=0;
            while (loc3 < this._aryEquipment.length) 
            {
                loc2 = this._aryEquipment[loc3];
                if (loc2 && loc2.category == ECategory._ECategoryEquip && !(loc2.type == EEquip._EEquipFashion)) 
                {
                    loc1 = this.getDurable(loc2);
                    if (loc1 <= arg1) 
                    {
                        return true;
                    }
                }
                ++loc3;
            }
            return false;
        }

        public resetPosition(): void
        {
            this.x = Global.stage.stageWidth - 290;
            this.y = 350;
            return;
        }

        public get toolTipData()
        {
            var loc1=null;
            if (this.isBreak) 
            {
                if (Cache.instance.vip.isVIP) 
                {
                    loc1 = this.ToolTipVIPBreakText;
                }
                else 
                {
                    loc1 = this.ToolTipBreakText;
                }
            }
            else if (Cache.instance.vip.isVIP) 
            {
                loc1 = this.ToolTipVIPWillBreakText;
            }
            else 
            {
                loc1 = this.ToolTipWillBreakText;
            }
            return HTMLUtil.addColor(loc1, "#FFFFFF");
        }

        public set toolTipData(arg1)
        {
            if (arg1 == null || arg1 == "") 
            {
                ToolTipsManager.unregister(this);
            }
            else 
            {
                ToolTipsManager.register(this);
            }
            return;
        }

        /* internal */ /* const */ToolTipWillBreakText: string=Language.getString(43858);

        /* internal */ /* const */ToolTipVIPWillBreakText: string=Language.getString(43859);

        /* internal */ /* const */ToolTipBreakText: string=Language.getString(43860);

        /* internal */ /* const */ToolTipVIPBreakText: string=Language.getString(43861);

        /* internal */ /* var */_equipmentRole: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_isloading: Boolean=false;

        /* internal */ /* var */_isBreak: Boolean=false;

        /* internal */ /* var */_isUpdate: Boolean=false;

        /* internal */ /* var */_aryEquipment: Array<any>;
    }
